/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jigdo.utils;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Paths;

/**
 * Contains helper methods for parsing binary files
 * @author John R Sohn
 */
public class FileUtils {
  
    
    public static boolean DeleteIfExists(String fileName) throws IOException
    {
        if (Files.exists(Paths.get(fileName), LinkOption.NOFOLLOW_LINKS))
        {
            Files.delete(Paths.get(fileName));
            return true;
        }
        else
        {
            return false;
        }
    }
  
    /**
     * Reads an integer length from an open file
     * @param file The file input stream.
     * @return The integer length
     * @throws java.io.IOException 
     */
     public static long GetIntFromFile(java.io.InputStream file, int length) throws java.io.IOException {
         
         byte[] buffer = new byte[length];
         
        file.read(buffer, 0, length);
        
        return ByteUtils.GetIntFromBytes(buffer,0,length);
    }
     
     public static byte[] ReadAllBytes(String Filename) throws IOException
     {
          java.io.FileInputStream fs = new java.io.FileInputStream(Filename);
          
          int i = fs.available();
          
          byte[] result = new byte[fs.available()];
          
          fs.read(result, 0, i);
          fs.close();
          
          return result;
     }
    
}
